﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using HardwareInterface;
using OpenCvSharp;

namespace VisualTargetDetection
{
    class VTDHost : IDisposable
    {
        private Thread workThread;
        public Detector Detector { get; private set; }
        public event Action<DetectionResult[]> onTargetDetected;
        private Camera cam;

        public VTDHost()
        {
            Detector = new Detector();
            workThread = new Thread(new ThreadStart(run));
            cam = new Camera();//初始化相机
            cam.Init();
        }

        private void run()
        {
            while (true)
            {
                Mat frame = cam.GetLatestFrame();
                var result = Detector.Detect(frame);
                if (result.Length > 0)
                    onTargetDetected?.Invoke(result);
                Thread.Sleep(0);
            }
        }

        public void Dispose()
        {
            workThread.Interrupt();
            Detector = null;
            cam.Abort();
        }
    }
}
